package com.iflytek.aisol.audit.iflytek.strategy.manage.common.entity;

import com.iflytek.aisol.audit.iflytek.strategy.manage.common.enums.RCode;
import com.iflytek.tjaipt.common.logger.Constant;
import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 * @author zxwu17
 */
@Data
public class R<T> implements Serializable {

    //编码：1成功，0和其它数字为失败
    private String code;
    //错误信息
    private String msg;
    //数据
    private T data;
    private String sid;

    private R() {
        this.sid = MDC.get(Constant.TRACE_LOG_MDC_KEY);
    }
    public static <T> R<T> success() {
        R<T> r = new R<>();
        r.setCode(RCode.SUCCESS.getCode());
        r.setMsg(RCode.SUCCESS.getDesc());
        return r;
    }

    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setCode(RCode.SUCCESS.getCode());
        r.setMsg(RCode.SUCCESS.getDesc());
        r.setData(data);
        return r;
    }

    public static <T> R<T> failure(String msg) {
        return failure(RCode.HINT_ERROR, msg);
    }

    public static <T> R<T> failure(RCode rCode) {
        R<T> r = new R<>();
        r.setCode(rCode.getCode());
        r.setMsg(rCode.getDesc());
        return r;
    }

    public static <T> R<T> failure(RCode rCode, Object... msg) {
        R<T> r = new R<>();
        r.setCode(rCode.getCode());
        r.setMsg(String.format(rCode.getDescFormat(), msg));
        return r;
    }


}
